home *** CD-ROM | disk | FTP | other *** search
/ HamCall (April 1991) / HAMCALL CD-ROM (Buckmaster)(April 1991).BIN / prgming / ctutor / chap04.txt < prev    next >
Text File  |  1990-10-14  |  30KB  |  703 lines

  1.  
  2.                                                      Chapter 4
  3.  
  4.                                  ASSIGNMENT & LOGICAL COMPARES
  5.  
  6.  
  7.  
  8. Throughout this chapter, references are given to various
  9. ranges of variables.  Your compiler may use a different range
  10. for some of the variables since the proposed ANSI standard
  11. does not define specific limits for all data types.  Consult
  12. the documentation for your compiler for the exact range for
  13. each of the variable types.
  14.  
  15.  
  16. INTEGER ASSIGNMENT STATEMENTS
  17. ______________________________________________________________
  18.  
  19. Load the file INTASIGN.C and display it for     ==============
  20. an example of assignment statements.  Three       INTASIGN.C
  21. variables are defined for use in the program    ==============
  22. and the rest of the program is merely a
  23. series of illustrations of various assignments.  The first two
  24. lines of the assignment statements assign numerical values to
  25. "a" and "b", and the next five lines illustrate the five basic
  26. arithmetic functions and how to use them.  The fifth is the
  27. modulo operator and gives the remainder if the two variables
  28. were divided.  It can only be applied to "int" or "char" type
  29. variables.  Following these, there are two lines illustrating
  30. how to combine some of the variables in some complex math
  31. expressions.  All of the above examples should require no
  32. comment except to say that none of the equations are meant to
  33. be particularly useful except as illustrations.  The "char"
  34. type variable will be defined in the description of the next
  35. example program.
  36.  
  37. The expressions in lines 17 and 18 are perfectly acceptable
  38. as given, but we will see later in this chapter that there is
  39. another way to write these for more compact code.
  40.  
  41.  
  42. VERY STRANGE LOOKING CODE
  43. ______________________________________________________________
  44.  
  45. This leaves us with the last two lines which may appear to you
  46. as being very strange.  The C compiler scans the assignment
  47. statement from right to left, (which may seem a bit odd since
  48. we do not read that way), resulting in a very useful
  49. construct, namely the one given here.  The compiler finds the
  50. value 20, assigns it to "c", then continues to the left
  51. finding that the latest result of a calculation should be
  52. assigned to "b".  Thinking that the latest calculation
  53. resulted in a 20, it assigns it to "b" also, and continues the
  54. leftward scan assigning the value 20 to "a" also.  This is a
  55. very useful construct when you are initializing a group of
  56. variables.  The last statement illustrates that it is possible
  57. to actually do some calculations to arrive at the value which
  58.  
  59.                                                            4-1
  60.  
  61.                      Chapter 4 - Assignment & Logical Compares
  62.  
  63. will be assigned to all three variables.  In fact, the
  64. rightmost expression can contain variables, even "a", "b", &
  65. "c".
  66.  
  67. The program has no output, so compiling and executing this
  68. program will be very uninteresting. Since you have already
  69. learned how to display some integer results using the "printf"
  70. function, it would be to your advantage to add some output
  71. statements to this program to see if the various statements
  72. do what you think they should do.
  73.  
  74.  
  75. DEFINITIONS FIRST THEN EXECUTABLE STATEMENTS
  76. ______________________________________________________________
  77.  
  78. This would be a good time for a preliminary definition of a
  79. rule to be followed in C.  The data definitions are always
  80. given before any executable statements in any program block.
  81. This is why the variables are defined first in this program
  82. and in every C program.  If you try to define a new variable
  83. after executing some statements, your compiler will issue an
  84. error.
  85.  
  86.  
  87. ADDITIONAL DATA TYPES
  88. ______________________________________________________________
  89.  
  90. Loading and editing MORTYPES.C will             ==============
  91. illustrate how some additional data types can     MORTYPES.C
  92. be used.  Once again we have defined a few      ==============
  93. integer type variables which you should be
  94. fairly familiar with by now, but we have added two new types,
  95. the "char", and the "float".
  96.  
  97. The "char" type of data is nearly the same as the integer
  98. except that it can only be assigned numerical values between
  99. -128 to 127 on most implementations of C, since it is stored
  100. in only one byte of memory.  The "char" type of data is
  101. usually used for ASCII data, more commonly known as text.  The
  102. text you are reading was originally written on a computer with
  103. a word processor that stored the words in the computer one
  104. character per byte.  In contrast, the integer data type is
  105. stored in two bytes of computer memory on nearly all
  106. microcomputers.
  107.  
  108.  
  109. DATA TYPE MIXING
  110. ______________________________________________________________
  111.  
  112. It would be profitable at this time to discuss the way C
  113. handles the two types "char" and "int".  Most functions in C
  114. that are designed to operate with integer type variables will
  115. work equally well with character type variables because they
  116. are a form of an integer variable.  Those functions, when
  117.  
  118.                                                            4-2
  119.  
  120.                      Chapter 4 - Assignment & Logical Compares
  121.  
  122. called on to use a "char" type variable, will actually promote
  123. the "char" data into integer data before using it.  For this
  124. reason, it is possible to mix "char" and "int" type variables
  125. in nearly any way you desire.  The compiler will not get
  126. confused, but you might.  It is good not to rely on this too
  127. much, but to carefully use only the proper types of data where
  128. they should be used.
  129.  
  130. The second new data type is the "float" type of data, commonly
  131. called floating point data.  This is a data type which usually
  132. has a very large range, a large number of significant digits,
  133. and a large number of computer words are required to store it.
  134. The "float" data type has a decimal point associated with it
  135. and has an allowable range of from 3.4E-38 to 3.4E+38 when
  136. using most C compilers on microcomputers, and is composed of
  137. about 7 significant digits.
  138.  
  139.  
  140. HOW TO USE THE NEW DATA TYPES
  141. ______________________________________________________________
  142.  
  143. The first three lines of the program assign values to all nine
  144. of the defined variables so we can manipulate some of the data
  145. between the different types.
  146.  
  147. Since, as mentioned above, a "char" data type is in reality
  148. an "integer" data type, no special considerations need be
  149. taken to promote a "char" to an "int", and a "char" type data
  150. field can be assigned to an "int" variable.  When going the
  151. other way, there is no standard, so you may simply get garbage
  152. if the value of the integer variable is outside the range of
  153. the "char" type variable.  Most C compilers simply truncate
  154. the most significant bits and use the 8 least significant
  155. bits.  It will translate correctly if the value is within the
  156. range of -128 to 127.
  157.  
  158. The third line illustrates the simplicity of translating an
  159. integer into a "float", simply assign it the new value and the
  160. system will do the proper conversion.  When going the other
  161. way however, there is an added complication.  Since there may
  162. be a fractional part of the floating point number, the system
  163. must decide what to do with it.  By definition, it will
  164. truncate it.
  165.  
  166. This program produces no output, and we haven't covered a way
  167. to print out "char" and "float" type variables, so you can't
  168. really get in to this program and play with the results, but
  169. the next program will cover this for you.
  170.  
  171. Be sure to compile and run this program after you are sure you
  172. understand it completely.
  173.  
  174.  
  175.  
  176.                                                            4-3
  177.  
  178.                      Chapter 4 - Assignment & Logical Compares
  179.  
  180. LOTS OF VARIABLE TYPES
  181. ______________________________________________________________
  182.  
  183. Load the file LOTTYPES.C and display it on      ==============
  184. your screen.  This file contains nearly every     LOTTYPES.C
  185. standard simple data type available in the      ==============
  186. programming language C.  There are other
  187. types, but they are the compound types (ie - arrays and
  188. structures) that we will cover in due time.
  189.  
  190. Observe the file.  First we define a simple "int", followed
  191. by a "long int" which has a range of -2147483648 to 2147483647
  192. with most C compilers, and a "short int" which has a range
  193. that is identical to that for the "int" variable, namely
  194. -32768 to 32767.  The "unsigned" is next and is defined as the
  195. same size as the "int" but with no sign.  The "unsigned" then
  196. will cover a range of 0 to 65535.  It should be pointed out
  197. that when the "long", "short", or "unsigned" is desired, the
  198. "int" is optional and is left out by most experienced
  199. programmers.  We have already covered the "char" and the
  200. "float", which leaves only the "double".  The "double" covers
  201. a greater range than the "float" and has more significant
  202. digits for more precise calculations.  It also requires more
  203. memory to store a value than the simple "float".  The "double"
  204. in most C compilers covers a range of 1.7E-308 to 1.7E+308.
  205.  
  206. Note that other compounding of types can be done such as "long
  207. unsigned int", "unsigned char", etc.  Check your documentation
  208. for a complete list of variable types.
  209.  
  210. Another diversion is in order at this point.  Your compiler
  211. has no provisions for floating point math, but only double
  212. floating point math.  It will promote a "float" to a "double"
  213. before doing calculations and therefore only one math library
  214. will be needed.  Of course, this is totally transparent to
  215. you, so you don't need to worry about it.  Because of this,
  216. you may think that it would be best to simply define every
  217. floating point variable as double, since they are promoted
  218. before use in any calculations, but that may not be a good
  219. idea.  A "float" variable requires 4 bytes of storage and a
  220. "double" requires 8 bytes of storage, so if you have a large
  221. volume of floating point data to store, the "double" will
  222. obviously require much more memory.
  223.  
  224. After defining the data types in the program under
  225. consideration, a numerical value is assigned to each of the
  226. defined variables in order to demonstrate the means of
  227. outputting each to the monitor.
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.                                                            4-4
  235.  
  236.                      Chapter 4 - Assignment & Logical Compares
  237.  
  238. SOME LATE ADDITIONS
  239. ______________________________________________________________
  240.  
  241. As any programming language evolves, additional constructs are
  242. added to fill some previously overlooked need.  Two new
  243. keywords have been added to C recently, and since they may not
  244. be a part of your compiler, they are not illustrated in
  245. example programs, but they will be discussed here.  The two
  246. new keywords are "const" and "volatile" and are used to tell
  247. the compiler that variables of these types are constants and
  248. cannot be changed by the program.  A constant declared with
  249. the const keyword declares a value that will never be changed
  250. by either the program or the system itself.  If volatile is
  251. used, it declares a value that will never be changed by the
  252. program but may be changed by some outside influence such as
  253. a clock update pulse incrementing a volatile type stored
  254. value.  Since these constants can never have a value assigned
  255. to them in the executable part of the program, they should
  256. always be initialized.
  257.  
  258. Examples of use in declaring constants of these two types are
  259. given as;
  260.  
  261.      const int index1 = 2;
  262.      const index2 = 6;
  263.      volatile const int index3 = 12;
  264.      volatile index4 = -23;
  265.  
  266. As mentioned earlier in this chapter, the keyword "int" is
  267. optional when used with these constant definitions
  268.  
  269.  
  270. THE CONVERSION CHARACTERS
  271. ______________________________________________________________
  272.  
  273. Following is a list of some of the conversion characters and
  274. the way they are used in the "printf" statement.  A complete
  275. list of all of the conversion characters should be included
  276. with the documentation for your compiler.
  277.  
  278.  
  279.      d   decimal notation
  280.      o   octal notation
  281.      x   hexadecimal notation
  282.      u   unsigned notation
  283.      c   character notation
  284.      s   string notation
  285.      f   floating point notation
  286.  
  287.  
  288. Each of these is used following a percent sign to indicate the
  289. type of output conversion, and between those two characters,
  290. the following  fields may be added.
  291.  
  292.                                                            4-5
  293.  
  294.                      Chapter 4 - Assignment & Logical Compares
  295.  
  296.      -     left justification in its field
  297.      (n)   a number specifying minimum field width
  298.      .     to separate n from m
  299.      (m)   significant fractional digits for a float
  300.      l     to indicate a "long"
  301.  
  302.  
  303. These are all used in the examples which are included in the
  304. program presently displayed on your monitor, with the
  305. exception of the string notation which will be covered later
  306. in this tutorial.  Note especially the variable field width
  307. specification demonstrated in lines 33 to 36.  This is not
  308. part of the original definition of C, but it is included in
  309. the proposed ANSI standard and will become part of the C
  310. language.  Compile and run this program to see what effect the
  311. various fields have on the output.
  312.  
  313. You now have the ability to display any of the data fields in
  314. the previous programs and it would be to your advantage to go
  315. back and see if you can display some of the fields anyway you
  316. desire.
  317.  
  318.  
  319. LOGICAL COMPARES
  320. ______________________________________________________________
  321.  
  322. Load and view the file named COMPARES.C for     ==============
  323. many examples of compare statements in C.  We     COMPARES.C
  324. begin by defining and initializing nine         ==============
  325. variables to use in the following compare
  326. statements.  This initialization is new to you and can be used
  327. to initialize variables while they are defined.
  328.  
  329. The first group of compare statements represents the simplest
  330. kinds of compares since they simply compare two variables.
  331. Either variable could be replaced with a constant and still
  332. be a valid compare, but two variables is the general case.
  333. The first compare checks to see if "x" is equal to "y" and it
  334. uses the double equal sign for the comparison.  A single equal
  335. sign could be used here but it would have a different meaning
  336. as we will see shortly.  The second comparison checks to see
  337. if "x" is greater than "z".
  338.  
  339. The third compare introduces the "NOT" operator, the
  340. exclamation, which can be used to invert the result of any
  341. logical compare.  The fourth checks for "b" less than or equal
  342. to "c", and the last checks for "r" not equal to "s".  As we
  343. learned in the last chapter, if the result of the compare is
  344. true, the statement following the "if" clause will be executed
  345. and the results are given in the comments.
  346.  
  347. Note that "less than" and "greater than or equal to" are also
  348. available, but are not illustrated here.
  349.  
  350.                                                            4-6
  351.  
  352.                      Chapter 4 - Assignment & Logical Compares
  353.  
  354. It would be well to mention the different format used for the
  355. "if" statement in this example program.  A carriage return is
  356. not required as a statement separator and by putting the
  357. conditional clause on the same line as the "if", it adds to
  358. the readability of the overall program.
  359.  
  360.  
  361. MORE COMPARES
  362. ______________________________________________________________
  363.  
  364. The compares in the second group are a bit more involved.
  365. Starting with the first compare, we find a rather strange
  366. looking set of conditions in the parentheses.  To understand
  367. this we must understand just what a "true" or "false" is in
  368. the C language.  A "false" is defined as a value of zero, and
  369. "true" is defined as a non-zero value.  Any integer or char
  370. type of variable can be used for the result of a true/false
  371. test, or the result can be an implied integer or char.
  372.  
  373. Look at the first compare of the second group of compare
  374. statements.  The expression "r != s" will evaluate as a "true"
  375. since "r" was set to 0.0 above, so the result will be a
  376. non-zero value.  With most C compilers, it would always be set
  377. to a 1, but you could get in trouble if you wrote a program
  378. that depended on it being 1 in all cases.  Good programming
  379. practice would be to not use the resulting 1 in any
  380. calculations.  Even though the two variables that are compared
  381. are "float" variables, the result will be of type "integer".
  382. There is no explicit variable to which it will be assigned so
  383. the result of the compare is an implied integer.  Finally the
  384. resulting number, probably 1 in this case, is assigned to the
  385. integer variable "x".  If double equal signs were used, the
  386. phantom value, namely 1, would be compared to the value of
  387. "x", but since the single equal sign is used, the value 1 is
  388. simply assigned to "x", as though the statement were not in
  389. parentheses.  Finally, since the result of the assignment in
  390. the parentheses was non-zero, the entire expression is
  391. evaluated as "true", and "z" is assigned the value of 1000.
  392. Thus we accomplished two things in this statement, we assigned
  393. "x" a new value, probably 1, and we assigned "z" the value of
  394. 1000.  We covered a lot in this statement so you may wish to
  395. review it before going on.  The important things to remember
  396. are the values that define "true" and "false", and the fact
  397. that several things can be assigned in a conditional
  398. statement.  The value assigned to "x" was probably a 1, but
  399. remember that the only requirement is that it is nonzero.
  400.  
  401. The next example should help clear up some of the above in
  402. your mind.  In this example, "x" is assigned the value of "y",
  403. and since the result is 11, the condition is non-zero, which
  404. is true, and the variable "z" is assigned 222.
  405.  
  406. The third example, in the second group, compares "x" to zero.
  407. If the result is true, meaning that if "x" is not zero, then
  408.  
  409.                                                            4-7
  410.  
  411.                      Chapter 4 - Assignment & Logical Compares
  412.  
  413. "z" is assigned the value of 333, which it will be.  The last
  414. example in this group illustrates the same concept, since the
  415. result will be true if "x" is non-zero.  The compare to zero
  416. is not actually needed and the result of the compare is true.
  417. The third and fourth examples of this group are therefore
  418. identical.
  419.  
  420.  
  421. ADDITIONAL COMPARE CONCEPTS
  422. ______________________________________________________________
  423.  
  424. The third group of compares will introduce some additional
  425. concepts, namely the logical "AND" and the logical "OR".  We
  426. assign the value of 77 to the three integer variables simply
  427. to get started again with some defined values.  The first
  428. compare of the third group contains the new control "&&",
  429. which is the logical "AND".  The entire statement reads, if
  430. "x" equals "y" AND if "x" equals 77 then the result is "true".
  431. Since this is true, the variable z is set equal to 333.
  432.  
  433. The next compare in this group introduces the "||" operator
  434. which is the "OR".  The statement reads, if "x" is greater
  435. than "y" OR if "z" is greater than 12 then the result is true.
  436. Since "z" is greater than 12, it doesn't matter if "x" is
  437. greater than "y" or not, because only one of the two
  438. conditions must be true for the result to be true.  The result
  439. is true, so therefore "z" will be assigned the value of 22.
  440.  
  441.  
  442. LOGICAL EVALUATION
  443. ______________________________________________________________
  444.  
  445. When a compound expression is evaluated, the evaluation
  446. proceeds from left to right and as soon as the result of the
  447. outcome is assured, evaluation stops.  Namely, in the case of
  448. an "AND" evaluation, when one of the terms evaluates to
  449. "false", evaluation is discontinued because additional true
  450. terms cannot make the result ever become "true".  In the case
  451. of an "OR" evaluation, if any of the terms is found to be
  452. "true", evaluation stops because it will be impossible for
  453. additional terms to cause the result to be "false".  In the
  454. case of additionally nested terms, the above rules will be
  455. applied to each of the nested levels.
  456.  
  457.  
  458. PRECEDENCE OF OPERATORS
  459. ______________________________________________________________
  460.  
  461. The question will come up concerning the precedence of
  462. operators.  Which operators are evaluated first and which
  463. last?  There are many rules about this topic, but I would
  464. suggest that you don't worry about it at this point.  Instead,
  465. use lots of parentheses to group variables, constants, and
  466. operators in a way meaningful to you.  Parentheses always have
  467.  
  468.                                                            4-8
  469.  
  470.                      Chapter 4 - Assignment & Logical Compares
  471.  
  472. the highest priority and will remove any question of which
  473. operations will be done first in any particular statements.
  474.  
  475. Going on to the next example in group three, we find three
  476. simple variables used in the conditional part of the compare.
  477. Since all three are non-zero, all three are "true", and
  478. therefore the "AND" of the three variables are true, leading
  479. to the result being "true", and "z" being assigned the value
  480. of 11.  Note that since the variables, "r", "s", and "t" are
  481. "float" type variables, they could not be used this way, but
  482. they could each be compared to zero and the same type of
  483. expression could be used.
  484.  
  485. Continuing on to the fourth example of the third group we find
  486. three assignment statements in the compare part of the "if"
  487. statement.  If you understood the above discussion, you should
  488. have no difficulty understanding that the three variables are
  489. assigned their respective new values, and the result of all
  490. three are non-zero, leading to a resulting value of "TRUE".
  491.  
  492.  
  493. THIS IS A TRICK, BE CAREFUL
  494. ______________________________________________________________
  495.  
  496. The last example of the third group contains a bit of a trick,
  497. but since we have covered it above, it is nothing new to you.
  498. Notice that the first part of the compare evaluates to
  499. "FALSE".  The remaining parts of the compare are not
  500. evaluated, because it is an "AND" and it will definitely be
  501. resolved as a "FALSE" because the first term is false.  If the
  502. program was dependent on the value of "y" being set to 3 in
  503. the next part of the compare, it will fail because evaluation
  504. will cease following the "FALSE" found in the first term.
  505. Likewise, "z" will not be set to 4, and the variable "r" will
  506. not be changed.
  507.  
  508.  
  509.  
  510. POTENTIAL PROBLEM AREAS
  511. ______________________________________________________________
  512.  
  513. The last group of compares illustrate three possibilities for
  514. getting into a bit of trouble.  All three have the common
  515. result that "z" will not get set to the desired value, but for
  516. different reasons.  In the case of the first one, the compare
  517. evaluates as "true", but the semicolon following the second
  518. parentheses terminates the "if" clause, and the assignment
  519. statement involving "z" is always executed as the next
  520. statement.  The "if" therefore has no effect because of the
  521. misplaced semicolon.  The second statement is much more
  522. straightforward because "x" will always be equal to itself,
  523. therefore the inequality will never be true, and the entire
  524. statement will never do a thing, but is wasted effort.  The
  525. last statement will always assign 0 to "x" and the compare
  526.  
  527.                                                            4-9
  528.  
  529.                      Chapter 4 - Assignment & Logical Compares
  530.  
  531. will therefore always be "false", never executing the
  532. conditional part of the "if" statement.
  533.  
  534. The conditional statement is extremely important and must be
  535. thoroughly understood to write efficient C programs.  If any
  536. part of this discussion is unclear in your mind, restudy it
  537. until you are confident that you understand it thoroughly
  538. before proceeding onward.  Compile and run this program.  Add
  539. some printout to see the results of some of the operations.
  540.  
  541.  
  542. THE CRYPTIC PART OF C
  543. ______________________________________________________________
  544.  
  545. There are three constructs used in C that        =============
  546. make no sense at all when first encountered        CRYPTIC.C
  547. because they are not intuitive, but they         =============
  548. greatly increase the efficiency of the
  549. compiled code and are used extensively by experienced C
  550. programmers.  You should therefore be exposed to them and
  551. learn to use them because they will appear in most, if not
  552. all, of the programs you see in the publications.  Load and
  553. examine the file named CRYPTIC.C for examples of the three new
  554. constructs.
  555.  
  556. In this program, some variables are defined and initialized
  557. in the same statements for use below.  The first executable
  558. statement simply adds 1 to the value of "x", and should come
  559. as no surprise to you.  The next two statements also add one
  560. to the value of "x", but it is not intuitive that this is what
  561. happens.  It is simply by definition that this is true.
  562. Therefore, by definition of the C language, a double plus sign
  563. either before or after a variable increments that variable by
  564. 1.  Additionally, if the plus signs are before the variable,
  565. the variable is  incremented before it is used, and if the
  566. plus signs are after the variable, the variable is used, then
  567. incremented.  In the next statement, the value of "y" is
  568. assigned to the variable "z", then "y" is incremented because
  569. the plus signs are after the variable "y".  In the last
  570. statement of the incrementing group of example statements, the
  571. value of "y" is incremented then its value is assigned to the
  572. variable "z".
  573.  
  574. The next group of statements illustrate decrementing a
  575. variable by one.  The definition works exactly the same way
  576. for decrementing as it does for incrementing.  If the minus
  577. signs are before the variable, the variable is decremented,
  578. then used, and if the minus signs are after the variable, the
  579. variable is used, then decremented.
  580.  
  581.  
  582.  
  583.  
  584.  
  585.                                                           4-10
  586.  
  587.                      Chapter 4 - Assignment & Logical Compares
  588.  
  589. THE CRYPTIC ARITHMETIC OPERATOR
  590. ______________________________________________________________
  591.  
  592. Another useful but cryptic operator is the arithmetic
  593. operator.  This operator is used to modify any variable by
  594. some constant value.  The first statement of the "arithmetic
  595. operator" group of statements simply adds 12 to the value of
  596. the variable "a".  The second statement does the same, but
  597. once again, it is not intuitive that they are the same.  Any
  598. of the four basic functions of arithmetic, "+", "-", "*", or
  599. "/", can be handled in this way, by putting the function
  600. desired in front of the equal sign and eliminating the second
  601. reference to the variable name.  It should be noted that the
  602. expression on the right side of the arithmetic operator can
  603. be any valid expression, the examples are kept simple for your
  604. introduction to this new operator.
  605.  
  606. Just like the incrementing and decrementing operators, the
  607. arithmetic operator is used extensively by experienced C
  608. programmers and it would pay you well to understand it.
  609.  
  610.  
  611. THE CONDITIONAL EXPRESSION
  612. ______________________________________________________________
  613.  
  614. The conditional expression is just as cryptic as the last two,
  615. but once again it can be very useful so it would pay you to
  616. understand it.  It consists of three expressions within
  617. parentheses separated by a question mark and a colon.  The
  618. expression prior to the question mark is evaluated to
  619. determine if it is "true" or "false".  If it is true, the
  620. expression between the question mark and the colon is
  621. evaluated, and if it is not true, the expression following the
  622. colon is evaluated.  The result of the evaluation is used for
  623. the assignment.  The final result is identical to that of an
  624. "if" statement with an "else" clause.  This is illustrated by
  625. the second example in this group.  The conditional expression
  626. has the added advantage of more compact code that will compile
  627. to fewer machine instructions in the final program.
  628.  
  629. The final two lines of this example program are given to
  630. illustrate a very compact way to assign the greater of two
  631. variables "a" or "b" to "c", and to assign the lessor of the
  632. same two variables to "c".  Notice how efficient the code is
  633. in these two examples.
  634.  
  635.  
  636. TO BE CRYPTIC OR NOT TO BE CRYPTIC
  637. ______________________________________________________________
  638.  
  639. Several students of C have stated that they didn't like these
  640. three cryptic constructs and that they would simply never use
  641. them.  This will be fine if they never have to read anybody
  642. else's program, or use any other programs within their own.
  643.  
  644.                                                           4-11
  645.  
  646.                      Chapter 4 - Assignment & Logical Compares
  647.  
  648. I have found many functions that I wished to use within a
  649. program but needed a small modification to use it, requiring
  650. me to understand another person's code.  It would therefore
  651. be to your advantage to learn these new constructs, and use
  652. them. They will be used in the remainder of this tutorial, so
  653. you will be constantly exposed to them.
  654.  
  655. This has been a long chapter but it contained important
  656. material to get you started in using C.  In the next chapter,
  657. we will go on to the building blocks of C, the functions.  At
  658. that point, you will have enough of the basic materials to
  659. allow you to begin writing meaningful programs.
  660.  
  661.  
  662. PROGRAMMING EXERCISES
  663. ______________________________________________________________
  664.  
  665. 1.   Write a program that will count from 1 to 12 and print
  666.      the count, and its square, for each count.
  667.          1   1
  668.          2   4
  669.          3   9   etc.
  670.  
  671. 2.   Write a program that counts from 1 to 12 and prints the
  672.      count and its inversion to 5 decimal places for each
  673.      count. This will require a floating point number.
  674.  
  675.          1   1.00000
  676.          2    .50000
  677.          3    .33333
  678.          4    .25000
  679.            etc.
  680.  
  681. 3.   Write a program that will count from 1 to 100 and print
  682.      only those values between 32 and 39, one to a line.
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.                                                           4-12
  702.  
  703.